const request = require('../../utils/request.js')
const util = require('../../utils/util.js')
const country = require('../../utils/countryCode.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    noData: false,
    backpackArr: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    var url = request.URL + '/backpack/list';
    request.request({
      url: url,
      method: 'POST',
      success: function(res) {
        console.log(res);
        var backpackArr = res.data;
        if (backpackArr.length > 0) {
          for (var i = 0; i < backpackArr.length; i++) {
            var startTime = util.formatTime(parseInt(backpackArr[i].startTime));
            var endTime = util.formatTime(parseInt(backpackArr[i].endTime));
            startTime = startTime.split(' ')[0];
            endTime = endTime.split(' ')[0];
            // console.log(startTime)
            var countryCode = backpackArr[i].countryId;
            var countryInfo = country.getCountryInfo(countryCode);
            var countryName = countryInfo.chineseName;
            backpackArr[i].startTime = startTime;
            backpackArr[i].endTime = endTime;
            backpackArr[i].countryName = countryName;
          }
          console.log(backpackArr)
          that.setData({
            backpackArr: backpackArr,
            noData: false
          })
        } else {
          that.setData({
            noData: true
          })
        }
      },
      fail: function() {
        request.showFailInfo()
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  toSearchPage: function(e) {
    console.log(e.currentTarget.dataset.title)
    wx.navigateTo({
      url: '../../pages/search/search' + '?storeId=' + -1,
    })
  },
  toBackpackDetail: function(e) {
    var selectedBackpackId = e.currentTarget.id;
    var that = this;
    var backpackArr = that.data.backpackArr;

    wx.navigateTo({
      url: './backpack_detail/backpack_detail?backpackId=' + selectedBackpackId
    })
  },
  //转发小程序
  onShareAppMessage: function() {
    return {
      title: '呆狗背包',
      path: '/pages/beibao/beibao ',
      success: function(res) {
        //转发成功
        wx.showToast({
          title: '转发成功',
          icon: 'success'
        })
      },
      fail: function(res) {
        //转发失败
      }
    }
  },
})